package com.sdry.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.Stagnant;
import com.sdry.entity.StagnantExample;
import com.sdry.entity.StagnantExample.Criteria;
import com.sdry.service.StagnantServise;
import com.sdry.utils.DateUtils;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName: StagnantControllerController
 * @Description: 呆滞物品管理
 * @author jyy
 * @date 2018年7月26日
 * @version V1.0
 */
@Controller
@RequestMapping("/stagnant")
public class StagnantController extends BaseController{

    @Resource
    private StagnantServise stagnantServise;


    /**
     * 去借调单列表页面
     * @return
     */
        @RequestMapping("/pageToStagnant")
        public String pageToStagnant() {

            return "/stagnant/stagnant";
    }

    /**
     * 添加借调管理
     * @param response 响应
     * @param stagnant 员工实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertStagnant")
    public void insertStagnant( HttpServletResponse response, Stagnant stagnant) throws Exception {

        //添加
        int affact = stagnantServise.insert(stagnant);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询员工信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param Stagnant 员工信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectStagnantList")
    public void selectStagnantList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "limit",required = false)int limit,
                                   @RequestParam(value = "offset",required = false)int offset,
                                   Stagnant stagnant) throws Exception {
    	PageHelper.startPage(offset, limit);
    	
    	StagnantExample example = new StagnantExample();
    	Criteria createCriteria = example.createCriteria();
    	
    	if(stagnant.getCremark12() != null && stagnant.getCremark12() != "") {
    		createCriteria.andCremark12Like("%"+stagnant.getCremark12()+"%");
    	}
    	
    	if(stagnant.getCremark9() != null && stagnant.getCremark9() != "") {
    		createCriteria.andCremark9EqualTo(stagnant.getCremark9());
    	}
    	
    	
		List<Stagnant> list = stagnantServise.selectByExample(example);
		//当前时间
		Date nowDate = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		for (Stagnant stagnant2 : list) {
			//入库时间不为空，计算呆滞时长
			if(stagnant2.getCremark9() != null && stagnant2.getCremark9() != "") {
				String cremark9 = stagnant2.getCremark9();
				Date enDate = dateFormat.parse(cremark9);
				String datePoor = DateUtils.getDatePoor(enDate, nowDate);
				
				stagnant2.setCremark13(datePoor);
			}
		}
		
		PageInfo<Stagnant> info = new PageInfo<Stagnant>(list);	
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据id查询员工信息
     * @param response 响应
     * @param id 员工id
     * @throws Exception 异常
     */
    @RequestMapping("/selectStagnantById")
    public void selectStagnantById(HttpServletResponse response, Long id) throws Exception {

        Stagnant stagnant = stagnantServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(stagnant);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改员工信息
     * @param response 响应
     * @param Stagnant 员工信息
     * @throws Exception 异常
     */
    @RequestMapping("/editStagnant")
    public void editStagnant( HttpServletResponse response,
                             Stagnant stagnant) throws Exception {

        //修改
        int affct = stagnantServise.updateByPrimaryKey(stagnant);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除员工
     * @param response 响应
     * @param ids 员工id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteStagnant")
    public void deleteStagnant( HttpServletResponse response, String ids) throws Exception {

        int affact = stagnantServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
    
   
}
